<template>
  <div class="register-container">
    <el-card class="register-box">
      <div class="logo">
        <img src="https://picsum.photos/100/100?random=2" alt="App Logo">
        <h2>用户注册</h2>
      </div>

      <el-form
          :model="registerForm"
          :rules="registerRules"
          ref="registerFormRef"
          @submit.native.prevent="handleRegister"
      >
        <el-form-item prop="username">
          <el-input
              v-model="registerForm.username"
              prefix-icon="el-icon-user"
              placeholder="请输入账号"
              clearable
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              v-model="registerForm.password"
              prefix-icon="el-icon-lock"
              placeholder="请输入密码"
              show-password
              clearable
          ></el-input>
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <el-input
              v-model="registerForm.confirmPassword"
              prefix-icon="el-icon-lock"
              placeholder="请确认密码"
              show-password
              clearable
          ></el-input>
        </el-form-item>

        <el-form-item prop="email">
          <el-input
              v-model="registerForm.email"
              prefix-icon="el-icon-message"
              placeholder="请输入邮箱"
              clearable
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              native-type="submit"
              :loading="loading"
              class="register-btn"
          >
            立即注册
          </el-button>
        </el-form-item>
      </el-form>

      <div class="footer-links">
        <router-link to="/login">已有账号? 立即登录</router-link>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'RegisterPage',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      loading: false,
      registerForm: {
        username: '',
        password: '',
        confirmPassword: '',
        email: ''
      },
      registerRules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}
        ],
        confirmPassword: [
          {required: true, message: '请确认密码', trigger: 'blur'},
          {validator: validatePassword, trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}
        ]
      }
    }
  },
  methods: {
    handleRegister() {
      this.$refs.registerFormRef.validate(valid => {
        if (valid) {
          this.loading = true

          // 模拟注册请求
          setTimeout(() => {
            this.$message.success('注册成功')
            this.loading = false
            this.$router.push('/login')
          }, 1500)
        }
      })
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.register-box {
  width: 450px;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.logo {
  text-align: center;
  margin-bottom: 30px;
}

.logo img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin-bottom: 15px;
}

.logo h2 {
  color: #333;
  font-size: 22px;
}

.register-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
}

.footer-links {
  text-align: center;
  margin-top: 20px;
}

.footer-links a {
  color: #999;
  text-decoration: none;
  font-size: 14px;
}

.footer-links a:hover {
  color: #409EFF;
}
</style>
